package com.shop.service.impl;

import com.shop.api.params.orderInfo;
import com.shop.dao.*;
import com.shop.entity.*;
import com.shop.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductAttributeMapper productAttributeMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StoreMapper storeMapper;

    private Long OId = 0L;
    private int Ostatus = 10;

    @Override
    public int createOrder(Long userId, Long storeId, String address, List<orderInfo> info) {
        for (orderInfo o : info) {
            ProductAttribute PA = productAttributeMapper.selectByPrimaryKey(o.getAttributeId());
            if (PA.getStock() < o.getGoodQuantity()) {
                return 50072;
            }
        }
        Order order = new Order();
        order.setUserId(userId);
        order.setStoreId(storeId);
        order.setAddress(address);
        order.setCreationTime(new Date());
        order.setOrderStatus(10);

        if (orderMapper.insertSelective(order) > 0) {
            boolean flag = true;
            for (orderInfo o : info) {
                ProductAttribute PA = productAttributeMapper.selectByPrimaryKey(o.getAttributeId());

                OrderProduct orderProduct = new OrderProduct();
                orderProduct.setOrderId(order.getOrderId());
                orderProduct.setProductId(o.getProductId());
                orderProduct.setUnitPrice(PA.getUnitPrice());
                orderProduct.setAttributeId(o.getAttributeId());
                orderProduct.setGoodQuantity(o.getGoodQuantity());

                if (orderProductMapper.insertSelective(orderProduct) > 0) {
                    if (productAttributeMapper.updateStockByPrimaryKey(o.getAttributeId(), o.getGoodQuantity()) > 0) {
                        log.info(order.toString());
                        flag = true;
                    } else {
                        flag = false;
                    }
                }
            }
            if (flag) {
                return 20000;
            }
        }
        return 50070;
    }

    @Override
    public Map searchOrder(Long orderId) {
        Map map = new HashMap<String, Object>();
        Order order = orderMapper.selectByPrimaryKey(orderId);
        List list = orderProductMapper.selectByOrderId(orderId);
        map.put("orderId", orderId);
        map.put("creationTime", order.getCreationTime());
        map.put("storeName", storeMapper.findStoreNameById(order.getStoreId()));
        map.put("address", order.getAddress());
        map.put("orderStatus", order.getOrderStatus());
        map.put("orderInfo", list);
        return map;
    }

    @Override
    public List searchUserAllOrder(User loginUser) {
        int u = loginUser.getUserType();
        if (u == 0) {
            List result = new ArrayList<>();
            List<Order> list = orderMapper.selectByUserId(loginUser.getUserId());
            for (Order o : list) {
                Map map = new HashMap<>();
                map.put("creationTime", o.getCreationTime());
                map.put("address", o.getAddress());
                map.put("orderId", o.getOrderId());
                map.put("storeName", storeMapper.findStoreNameById(o.getStoreId()));
                map.put("orderStatus", o.getOrderStatus());
                List<OrderProduct> list1 = orderProductMapper.selectByOrderId(o.getOrderId());
                List l = new ArrayList<>();
                for (OrderProduct op : list1) {
                    Map m = new HashMap<>();
                    m.put("unitPrice", op.getUnitPrice());
                    m.put("productId", op.getProductId());
                    m.put("goodQuantity", op.getGoodQuantity());
                    String productName=productMapper.selectByPrimaryKey(op.getProductId()).getProductName();
                    m.put("productName",productName);
                    ProductAttribute p = productAttributeMapper.selectByPrimaryKeyIncludeDelete(op.getAttributeId());
                    m.put("color", p.getColor());
                    m.put("kit", p.getKit());
                    m.put("imgPath", p.getImgPath());
                    l.add(m);
                }
                map.put("info", l);
                result.add(map);
            }
            return result;
        } else {
            List result = new ArrayList<>();
            List<Store> stores = storeMapper.findAllStoreByUserId(loginUser.getUserId());
            for (Store s : stores) {
                List resultOrder = new ArrayList<>();
                Map mapStore = new HashMap<>();
                mapStore.put("storeId", s.getStoreId());
                mapStore.put("storeName", s.getStoreName());
                List<Order> list = orderMapper.selectByStoreId(s.getStoreId());
                for (Order o : list) {
                    Map map = new HashMap<>();
                    map.put("creationTime", o.getCreationTime());
                    map.put("address", o.getAddress());
                    map.put("orderId", o.getOrderId());
//                    map.put("storeName", storeMapper.findStoreNameById(o.getStoreId()));
                    map.put("orderStatus", o.getOrderStatus());
                    map.put("userId", o.getUserId());
                    List<OrderProduct> list1 = orderProductMapper.selectByOrderId(o.getOrderId());
                    List l = new ArrayList<>();
                    for (OrderProduct op : list1) {
                        Map m = new HashMap<>();
                        m.put("unitPrice", op.getUnitPrice());
                        m.put("productId", op.getProductId());
                        m.put("goodQuantity", op.getGoodQuantity());
                        String productName=productMapper.selectByPrimaryKey(op.getProductId()).getProductName();
                        m.put("productName",productName);
                        ProductAttribute p = productAttributeMapper.selectByPrimaryKeyIncludeDelete(op.getAttributeId());
                        m.put("color", p.getColor());
                        m.put("kit", p.getKit());
                        m.put("imgPath", p.getImgPath());
                        l.add(m);
                    }
                    map.put("info", l);
                    resultOrder.add(map);
                }
                mapStore.put("info", resultOrder);
                result.add(mapStore);
            }
            return result;
        }
    }

    //支付    10 待支付    12 支付失败
    //发货    20 待发货
    //收货    30 待收货  31 待签收
    //完成    40 已完成
    @Override
    public int changeStatus(Long orderId, int status) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order.getOrderStatus() > status) {
            return 50074;
        } else if (order.getOrderStatus() == status) {
            return 50075;
        }
        OId = orderId;
        Ostatus = order.getOrderStatus();
        if (Ostatus == 12) {
            return 50076;
        }
        return getNextStatus(status);
    }

    private int getNextStatus(int status) {
        switch (status) {
            case 12:
                return goPayStatus(status);
            case 20:
                return goToBeShippedStatus(status);
            case 30:
            case 31:
                return goToBeReceivedStatus(status);
            case 40:
                return goFinishStatus(status);
        }
        return 50073;
    }

    private int goPayStatus(int status) {
        switch (status) {
            case 12:
                if (orderMapper.updateStatusByPrimaryKey(OId, status) > 0) {
                    return 20000;
                }
        }
        return 50073;
    }

    //    待发货状态
    private int goToBeShippedStatus(int status) {
        switch (status) {
            case 20:
                if (orderMapper.updateStatusByPrimaryKey(OId, status) > 0) {
                    return 20000;
                }
        }
        return 50073;
    }

    //    待收货状态
    private int goToBeReceivedStatus(int status) {
        if (Ostatus == 20 && status == 31) {
            return 50079;
        }
        switch (status) {
            case 30:
            case 31:
                if (orderMapper.updateStatusByPrimaryKey(OId, status) > 0) {
                    return 20000;
                }
        }
        return 50073;
    }

    //    已完成状态
    private int goFinishStatus(int status) {
        if (Ostatus > 20 && Ostatus < 30) {
            return 50079;
        }
        if (Ostatus == 30) {
            return 50080;
        }
        switch (status) {
            case 12:
            case 40:
                if (orderMapper.updateStatusByPrimaryKey(OId, status) > 0) {
                    return 20000;
                }

        }
        return 50073;
    }
}
